<!-- 开门记录列表 -->
<template>
	<view>
		<view class="no-warpper" v-if="isLong">
			<!-- 有数据 渲染列表 -->
			<view v-if="hasInfo">
				<view class="scroll-box">
					<view class="nl-shadow-box card-box" v-for="(item,index) in list" :key="index">
						<view class="nl-flex-list">
							<view class="img-open-door">
								<u-image width="100%" @click="imgPreview(item.pictureBase64)" height="100%"
									:src="item.pictureBase64"></u-image>
							</view>
							<view class="content">
								<view class="item" style="margin-bottom: 40rpx;">
									<view>时间</view>
									<text>：</text>
									<view>{{item.dateTime}}</view>
								</view>
								<view class="item">
									<view>位置</view>
									<text>：</text>
									<view>{{item.position}}</view>
								</view>
							</view>
						</view>
					</view>

					<u-loadmore :status="status" />
				</view>
			</view>
			<!-- 无数据 渲染缺省图 -->
			<view v-if="hasInfo == false">
				<noData :noDataText="noDataText"></noData>
			</view>
		</view>
		<view v-else>
			<netError v-if="!hasNet" @refresh="refresh()"></netError>
		</view>
	</view>

</template>

<script>
	import {
		getOpenLogsList
	} from 'api/treasure';
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				listBox: [], // 避免上拉刷新时，页面出现空白
				list: [],
				status: 'loadmore',

				isLong: false, // 是否加载完成
				hasNet: true, // 是否有网络
				hasInfo: '', // 是否有数据
				noDataText: '暂无数据~',
				pamras: {}, //查询条件

				page: 1,
				records: 0
			}
		},
		computed: {
			...mapState('user', ['userInfo', 'userCommunity'])
		},
		onLoad(option) {
			let pamras = JSON.parse(option.pamras)
			this.pamras = JSON.parse(option.pamras)
			this.pamras['row'] = 10
			this.pamras['communityId'] = this.userCommunity.communityId
			this.page = 1
			this.records = 0
			this.getOpenLogsList(1)
			console.log('this.pamras', this.pamras)
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.listBox = []
			this.getOpenLogsList()
		},
		onReachBottom() {
			console.log('页面触底了')
			if (this.status == 'loading') { // 防止多次触发

			} else {
				this.status = 'loading'
				if (this.pamras.page <= this.records) {
					this.getOpenLogsList(this.page)
				} else {
					this.status = 'nomore'
				}
			}
		},
		methods: {
			/**
			 * 获取开门记录
			 */
			getOpenLogsList(page) {
				var that = this
				that.pamras['page'] = page

				getOpenLogsList(that.pamras).then((res) => {
					console.log('获取开门记录', res)
					if (res.code == 200) {
						if(res.data.records == 0){ //暂无数据
							that.hasInfo = false
							that.isLong = true;
							uni.hideLoading()
							uni.stopPullDownRefresh()
							return;
						}
						
						that.records = res.data.records
						if (that.records == that.page) {
							that.status = 'nomore'
						} else {
							that.status = 'loadmore'
						}

						that.page++
						uni.stopPullDownRefresh()
						for (let i of res.data.gateOpenLogVoList) {
							that.listBox.push(i)
						}
						that.list = that.listBox
						that.isLong = true
						that.hasInfo = true
					}
				}).catch(function(reason) {
					uni.stopPullDownRefresh()
					// 处理网络错误
					that.hasNet = false
					that.isLong = false
				})
			},
			/**
			 * 刷新
			 */
			refresh() {
				this.page = 1
				this.records = 0
				this.list = []
				this.getOpenLogsList(1)
			},
			/**
			 * 图片预览
			 */
			imgPreview(val) {
				var urlList = []
				urlList.push(val)
				uni.previewImage({
					indicator: "number",
					loop: true,
					urls: urlList
				})
			}
		}
	}
</script>

<style lang="scss">
	.card-box .content {
		background: none;
	}

	.scroll-box {
		width: 100%;
		padding-bottom: 32rpx;
	}

	.card-box .content .item view:nth-child(1) {
		width: 110rpx;
	}

	.img-open-door {
		width: 220rpx;
		height: 250rpx;
		margin: 24rpx 0;
	}
</style>
